<template>
  <div class="login-body">
    <div class="login-container">
      <div class="head">
        <img class="logo" src="https://s.yezgea02.com/1582958061265/mlogo.png"/>
        <div class="name">
          <div class="title">新蜂商城</div>
          <div class="tips">Vue3.0 后台管理系统</div>
        </div>
      </div>
      <el-form label-position="top" :rules="state.rules" :model="state.ruleForm" ref="loginForm" class="login-form">
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model.trim="state.ruleForm.username" autocomplete="off"
                    placeholder="请输入账号"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model.trim="state.ruleForm.password" autocomplete="off"
                    placeholder="请输入密码"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="验证码" prop="imgCode">
          <el-input type="password" v-model.trim="state.ruleForm.imgCode" autocomplete="off"
                    placeholder="请输入验证码"></el-input>
        </el-form-item>
        <img class="logo" :src="mapSrc"/>
        <br>
        <el-form-item>
          <el-checkbox v-model="state.checked" @change="!state.checked"><a href="www.baidu.com"
                                                                           style="text-decoration: none">同意我们的《服务条款》</a>
          </el-checkbox>
          <el-button style="width: 100%" type="warning" @click="getCode">获取验证码</el-button>
          <hr/>
          <el-button style="width: 100%" type="primary" @click="submitForm">立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {getImgCode, logins} from "@/api/sys";
import {useStore} from "vuex";
import {useRouter} from "vue-router";

const router = useRouter()
const store = useStore()
const loginForm = ref(null)
const mapSrc = ref('')
const state = reactive({
  ruleForm: {
    username: '',
    password: '',
    imgCode: ''
  },
  checked: true,
  rules: {
    username: [
      {required: 'true', message: '账户不能为空', trigger: 'blur'}
    ],
    password: [
      {required: 'true', message: '密码不能为空', trigger: 'blur'}
    ],
    imgCode: [
      {required: 'true', message: '验证码不能为空', trigger: 'blur'}
    ]
  }
})

const submitForm = async () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      logins({
        username: state.ruleForm.username,
        password: state.ruleForm.password,
        imgCode: state.ruleForm.imgCode
      }).then((res) => {
            // console.log(res)

            store.commit('set_token', res)
            router.push({
              path: 'home'
            })
          }
      )
    } else {
      console.log('error submit!!')
      return false;
    }
  })
}

function getCode() {
  getImgCode(state.ruleForm.username).then((res) => {
    console.log(res)
    mapSrc.value = window.URL.createObjectURL(res)
  })
}
</script>

<style scoped>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: peru;
}

.login-container {
  position: relative;
  top: 141px;
  width: 420px;
  height: 570px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}

.head {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 20px 0;
}

.head img {
  width: 100px;
  height: 100px;
  margin-right: 20px;
}

.head .title {
  font-size: 28px;
  color: #1BAEAE;
  font-weight: bold;
}

.head .tips {
  font-size: 12px;
  color: #999;
}

.login-form {
  width: 70%;
  margin: 0 auto;
}

.login-form >>> .el-form--label-top .el-form-item__label {
  padding: 0;
}

.login-form >>> .el-form-item {
  margin-bottom: 0;
}
</style>
